const shellHelper = require('../libs/shellHelper')
const inquirer = require('inquirer');

const inquirerFind = async (dirPath, goalStr) => {
  const promptList = [{
    type: 'input',
    message: '请输入文件或目录地址:',
    name: 'dirPath',
    validate: function (val) {
      if (val.match(/^$|(^\s+)|(\s+$)/g)) { // 非空非头尾空格
        return "请正确输入";
      }
      return true;
    }
  },
  {
    type: 'input',
    message: '请输入搜索字符串:',
    name: 'goalStr',
    validate: function (val) {
      if (val.match(/^$|(^\s+)|(\s+$)/g)) { // 非空非头尾空格
        return "请正确输入";
      }
      return true;
    }
  }]
  const fileFind = (dirPath, goalStr) => {
    // spinner.start(`${dirPath}查找中`)
    shellHelper.series([
      `node ../libs/find ${dirPath} ${goalStr}`,
    ])
    // code === 0 && spinner.succeed(`${dirPath}查找成功`)
  }
  if (!dirPath) {
    const result = await inquirer.prompt(promptList)
    dirPath = result.dirPath
    goalStr = result.goalStr
  }

  fileFind(dirPath, goalStr)
}

module.exports = {
  inquirerFind
} 
// /Users/roft/Desktop/workspace/linyi-cli/libs "var exc = new RegExp\(lookingForString\)"